package {
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.media.Camera;
	import flash.media.Video;
	import flash.text.TextField;
	import flash.utils.ByteArray;
	
	import org.libspark.flartoolkit.core.FLARCode;
	import org.libspark.flartoolkit.core.param.FLARParam;
	import org.libspark.flartoolkit.core.raster.rgb.FLARRgbRaster_BitmapData;
	import org.libspark.flartoolkit.detector.FLARSingleMarkerDetector;
	
	public class Pong extends Sprite {
		[Embed(source="../lib/camera_para.dat", mimeType="application/octet-stream")]
		private var CameraParameters:Class;
		[Embed(source="../lib/square.pat", mimeType="application/octet-stream")]
		private var MarkerPattern:Class;
		private var cameraParameters:FLARParam;
		private var markerPattern:FLARCode;
		private var raster:FLARRgbRaster_BitmapData;
		private var detector:FLARSingleMarkerDetector;
		private var camera:Camera;
		private var video:Video;
		private var snapshot:BitmapData;
		private var myTextField:TextField;
		private var lastX:int = 0;
		private var lastY:int = 0;
		private var lastLastX:int = -1;
		private var lastLastY:int = -1;
		private var actualX:int;
		private var actualY:int;
		
		public function Pong() {
			
			// Camera init
			this.camera = Camera.getCamera();
			this.camera.setMode(640, 480, 60, false);
			this.video = new Video(640, 480);
			this.video.attachCamera(camera);
			this.snapshot = new BitmapData(video.width, video.height, false, 0x0);
			this.addChild( video );
			
			// Flar init
			this.cameraParameters = new FLARParam();
			this.cameraParameters.loadARParam(new CameraParameters() as ByteArray);
			this.markerPattern = new FLARCode(16, 16);
			this.markerPattern.loadARPatt(new MarkerPattern());
			detector = new FLARSingleMarkerDetector(cameraParameters, markerPattern, 80);
			
			// TextFiled init
			myTextField = new TextField();
			myTextField.background = true;
			myTextField.selectable = false;
			//myTextField.text = "Chupala!";
			this.addChild(myTextField);
			
			// Loop init
			this.addEventListener(Event.ENTER_FRAME, loop);
		}
		
		private function loop(e:Event):void {
			snapshot.draw(video);
//			snapshot = EdgeDetection.bluescaleFilter( snapshot );
//			snapshot = EdgeDetection.smothFilter3x3( snapshot );
//			snapshot = EdgeDetection.gradientMagnitudeFilter( snapshot );
			raster = new FLARRgbRaster_BitmapData( snapshot );
			if (detector.detectMarkerLite(raster, 80) && detector.getConfidence() > 0.3 ) {
            	//myTextField.autoSize = TextFieldAutoSize.LEFT;
				this.actualX = this.getX();
				this.actualY = this.getY();
				myTextField.text = "Detected!";
			}else{
				if( this.lastLastX != -1 && this.lastX != -1 && 
					this.lastLastY != -1 && this.lastY != -1) {
					this.actualX = (this.lastX - this.lastLastX) + this.lastX;
					this.actualY = (this.lastY - this.lastLastY) + this.lastY;
					myTextField.text = "Me muevo solo!";
				}else if( this.lastLastX != -1 && this.lastLastY != -1 ) {
					this.actualX = this.lastX;
            		this.actualY = this.lastY;
            		myTextField.text = "Me quedo donde estaba!";
				}else{
					this.actualX = 0;
					this.actualY = 0;
				}
			}
			this.lastLastX = this.lastX;
			this.lastLastY = this.lastY;
			this.lastX = actualX;
			this.lastY = actualY;
			myTextField.x = this.actualX;
            myTextField.y = this.actualY;
		}
		
		private function getX():int {
			return (detector.getSquare().imvertex[0].x + detector.getSquare().imvertex[1].x +
				detector.getSquare().imvertex[2].x + detector.getSquare().imvertex[3].x) / 4;
		}
		
		private function getY():int {
			return (detector.getSquare().imvertex[0].y + detector.getSquare().imvertex[1].y +
				detector.getSquare().imvertex[2].y + detector.getSquare().imvertex[3].y) / 4;
		}
	}
}
